package main

import (
	uuid "github.com/satori/go.uuid"
	"gogogo-testing-ground/model"
	"gogogo-testing-ground/utils"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

func main() {
	//连接并创建数据库
	db, err := gorm.Open(sqlite.Open("gogogo.db"), &gorm.Config{})
	if err != nil {
		panic("连接数据库失败")
	}
	//自动建表
	err = db.AutoMigrate(&model.SysUser{}, &model.SysRole{})
	if err != nil {
		panic("建表失败")
	}
	//解析一个json 文件数据
	var sysUserList SysUserList
	err = utils.ParseJsonFile("assets/user.json", &sysUserList)
	if err != nil {
		panic("解析json数据失败")
	}
	for _, obj := range sysUserList.Users {
		obj.UUID = uuid.NewV4()
		obj.Password = utils.BcryptHash(obj.Password)
	}
	//添加数据
	if err = db.Create(&sysUserList.Users).Error; err != nil {
		panic("添加数据出错")
	}

}

type SysUserList struct {
	Users []*model.SysUser
}
